//
//  ParseStatusXMLReader.m
//  cliqset
//
//  Created by David Hinson on 3/2/09.
//  Copyright 2009 Cliqset, Inc.. All rights reserved.
//

#import "ParseStatusXMLReader.h"
#import "Status.h"

@implementation ParseStatusXMLReader

@synthesize mode                     = _mode;
@synthesize contentOfCurrentProperty = _contentOfCurrentProperty;
@synthesize currentStatusObject      = _currentStatusObject;

NSString *strStatusEntry     = @"StatusEntry";
NSString *strStatus          = @"Status";
NSString *strUpdated         = @"Updated";

-(id)init {
	[super init];
	
	return self;
}

-(void)resetVars {
	
	self.contentOfCurrentProperty = nil;
	
	if (@selector(_currentStatusObject) && _currentStatusObject!=nil && self.currentStatusObject) {
		[self.currentStatusObject release];
		self.currentStatusObject = nil;
	}
}

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
	[self resetVars];
}

- (void)parseXMLFileAtData:(NSData *)data parseError:(NSError **)error
{	
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
	
    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [parser setDelegate:self];
	
    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    
    [parser parse];
    
    NSError *parseError = [parser parserError];
    if (parseError && error) {
        *error = parseError;
    }
	
	[self resetVars];
    
    [parser release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if (qName) {
        elementName = qName;
    }
	
	if ([elementName isEqualToString:strStatusEntry]) {		
		
		[self resetVars];
		
		self.currentStatusObject               = [[Status alloc] init];
		
	} else if ([elementName isEqualToString:strStatus]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else if ([elementName isEqualToString:strUpdated]) {
        self.contentOfCurrentProperty = [NSMutableString string];
    } else {
        // The element isn't one that we care about, so set the property that holds the 
        // character content of the current element to nil. That way, in the parser:foundCharacters:
        // callback, the string that the parser reports will be ignored.
        self.contentOfCurrentProperty = nil;
	}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{     
	
    if (qName) {
        elementName = qName;
    }
	
	
	// Status
    if ([elementName isEqualToString:strStatus]) {
		if (self.contentOfCurrentProperty) {
			self.currentStatusObject.status = self.contentOfCurrentProperty;
		} else {
			self.currentStatusObject.status = (NSMutableString*)@"";
		}
	}
	
	// Updated
    if ([elementName isEqualToString:strUpdated]) {
		if (self.contentOfCurrentProperty) {
			self.currentStatusObject.timeStamp = self.contentOfCurrentProperty;
		} else {
			self.currentStatusObject.timeStamp = (NSMutableString*)@"";
		}
	}
	
	if ([elementName isEqualToString:strStatusEntry]) {
		// we will write the Status to an array here...
		if (self.currentStatusObject) {
			self.currentStatusObject.mode = self.mode;
			[(id)[[UIApplication sharedApplication] delegate] performSelectorOnMainThread:@selector(addStatus:) withObject:self.currentStatusObject waitUntilDone:YES];
		}
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (self.contentOfCurrentProperty) {
        // If the current element is one whose content we care about, append 'string'
        // to the property that holds the content of the current element.
		if (string) {
			string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
			string = [string stringByReplacingOccurrencesOfString:@"\r" withString:@""];
			string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
			[self.contentOfCurrentProperty appendString:string];
		}
    }
}

-(void) dealloc {
	[_contentOfCurrentProperty release];
	[_currentStatusObject release];	
	[super dealloc];
}

@end
